API Documentation
Public Member Functions | List of all members
nkGraphics::Program Class Referenceabstract

Holds all informations related to GPU programs, and manages their compilation. More...

Inheritance diagram for nkGraphics::Program:
nkGraphics::Resource nkExport::Exportable

Public Member Functions

 Program ()
 
virtual ~Program ()
 
const ProgramCompilationResultgetLastCompilationResult () const
 
bool isFromFile () const
 
bool isFromMemory () const
 
ProgramSourcesHoldergetMemory () const
 
const InfiniteByteMaskgetSemantics () const
 
unsigned int getId () const
 
PROGRAM_TYPE getType () const
 
PROGRAM_COMPILATION_PROFILE getProfile () const
 
virtual void setFromMemory (const ProgramSourcesHolder &memory)
 
virtual void setDefines (const ProgramSourcesHolder &memory)
 
void setFromFiles (const nkMemory::StringView &folder)
 
void setFileExtensions (const nkMemory::StringView &value)
 
void setId (unsigned int id)
 
void setProfile (PROGRAM_COMPILATION_PROFILE profile)
 
void setActivateCrossCompilerDefines (bool value)
 
bool prepareMemoryWithFiles ()
 
virtual bool loadFromMemory ()=0
 
virtual bool tryToLoadFrom (ProgramSourcesHolder &newMemory)=0
 
virtual bool load () override
 
virtual void unload () override
 
virtual void exportClassToTree (nkExport::Node *rootNode) override
 
virtual void importClassFromTree (nkExport::Node *rootNode) override
 
- Public Member Functions inherited from nkGraphics::Resource
 Resource ()
 
 Resource (const nkMemory::StringView &path)
 
virtual ~Resource ()
 
bool isReadyForRendering () const
 
bool isUnloaded () const
 
RESOURCE_LOAD_STATE getLoadState () const
 
nkMemory::StringView getResourcePath () const
 
RESOURCE_TYPE getResourceTypeName () const
 
nkMemory::StringView getResourceName () const
 
bool getHidden () const
 
bool getGpuUploadCanBeDeferred () const
 
virtual void setResourcePath (const nkMemory::StringView &path)
 
void setResourceName (const nkMemory::StringView &name)
 
void setHidden (bool value)
 
void setGpuUploadCanBeDeferred (bool value)
 
- Public Member Functions inherited from nkExport::Exportable
 Exportable ()
 
virtual ~Exportable ()
 

Detailed Description

Holds all informations related to GPU programs, and manages their compilation.

Constructor & Destructor Documentation

◆ Program()

nkGraphics::Program::Program ( )

Constructor. See ShaderManager::createOrRetrieveProgram().

◆ ~Program()

virtual nkGraphics::Program::~Program ( )
virtual

Destructor.

Member Function Documentation

◆ getLastCompilationResult()

const ProgramCompilationResult& nkGraphics::Program::getLastCompilationResult ( ) const
Returns
The last compilation result. Generated whenever a loading is called.

◆ isFromFile()

bool nkGraphics::Program::isFromFile ( ) const
Returns
Whether the program has been made from files (true) or not (false).

◆ isFromMemory()

bool nkGraphics::Program::isFromMemory ( ) const
Returns
Whether the program has been made from an internal memory (true) or not (false).

◆ getMemory()

ProgramSourcesHolder* nkGraphics::Program::getMemory ( ) const
Returns
The currently used sources memory.

◆ getSemantics()

const InfiniteByteMask& nkGraphics::Program::getSemantics ( ) const
Returns
The semantics of the program.

◆ getId()

unsigned int nkGraphics::Program::getId ( ) const
Returns
The id of the program.

◆ getType()

PROGRAM_TYPE nkGraphics::Program::getType ( ) const
Returns
The type of program. Detected, depending on what kind of memory is loaded.

◆ getProfile()

PROGRAM_COMPILATION_PROFILE nkGraphics::Program::getProfile ( ) const
Returns
The profile use by the program.

◆ setFromMemory()

virtual void nkGraphics::Program::setFromMemory ( const ProgramSourcesHolder memory)
virtual

Sets the program from an already loaded memory set.

The memory used will need to fit some constraints for a Program to find back relevant data :

  • The function to execute in a program needs to be named "main", for all stages except raytracing.
  • For raytracing stage, depending on their role, entry functions should be named : "miss", "raygen", "closestHit".
Parameters
memoryThe memory to use.

◆ setDefines()

virtual void nkGraphics::Program::setDefines ( const ProgramSourcesHolder memory)
virtual

Sets the defines to use within the program. Can be useful to add defines and then load a program from a folder.

Parameters
memoryThe memory to copy de defines from.

◆ setFromFiles()

void nkGraphics::Program::setFromFiles ( const nkMemory::StringView folder)

Sets the folder from which the program should load its sources. The files that can be loaded are :

  • vertexShader.x -> vertex stage
  • hullShader.x -> hull stage
  • domainShader.x -> domain stage
  • geometryShader.x -> geometry stage
  • pixelShader.x -> pixel stage
  • computeShader.x -> compute stage
  • raytracingShader.x -> raytracing stage

With x being the extension set through the setFileExtensions() function, or the default which depends on the type of renderer :

Parameters
folderThe path of the folder to load from, relative to the working directory. See nkResources::ResourceManager for the path naming conventions.

◆ setFileExtensions()

void nkGraphics::Program::setFileExtensions ( const nkMemory::StringView value)

Sets the extension to search for when parsing the given folder. This allows to override the default behavior of the file seeking algorithm, enabling to put in common files loaded for all type of renderers, for instance.

Parameters
valueThe extension to append to the file names.

◆ setId()

void nkGraphics::Program::setId ( unsigned int  id)

Sets the id of the program.

Parameters
idThe id to use.

◆ setProfile()

void nkGraphics::Program::setProfile ( PROGRAM_COMPILATION_PROFILE  profile)

Sets the profile to use for compilation.

Parameters
profileThe profile to use.
Remarks
This will drive the version of the compiler used by the program sources.
For HLSL sources, this will call the compilation functions with the given profile as an argument.
For GLSL sources, this will prefill the first line of the shader with the right #version. As such, this should not be provided by client code. Note : currently GLSL will only be compiled with version 330.

◆ setActivateCrossCompilerDefines()

void nkGraphics::Program::setActivateCrossCompilerDefines ( bool  value)

Activates or not the cross compiler defines feeding. This is a good tool to use if you plan on making your programs work both on DirectX and OpenGL based renderers. Setting this to true will enable keywords within hlsl / glsl sources to ease cross-platform development. By default, this flag is off to lower processing times while loading programs.
If set to on, new keywords will become available to program sources, that will translate correctly into both languages :

Nk keyword Hlsl translation Glsl translation

Types
nkBool2 bool2 bvec2
nkBool3 bool3 bvec3
nkBool4 bool4 bvec4
nkFloat2 float2 vec2
nkFloat3 float3 vec3
nkFloat4 float4 vec4
nkInt2 int2 ivec2
nkInt3 int3 ivec3
nkInt4 int4 ivec4
nkUint2 uint2 uvec2
nkUint3 uint3 uvec3
nkUint4 uint4 uvec4
nkFloat2x2 float2x2 mat2
nkFloat3x3 float3x3 mat3
nkFloat4x4 float4x4 mat4
nkFloat2x4 float2x4

mat2x4

Textures types
nkTexture2d(name, slot) Texture2D [name] : register(t[slot]) layout(binding = [slot]) uniform sampler2D [name]
nkTextureCube(name, slot) TextureCube [name] : register(t[slot]) layout(binding = [slot]) uniform samplerCube [name]
nkSampler(name, slot) SamplerState [name] : register(s[slot])

bool [name] (no valid translation)

Textures functions
nkSample(tex, sampler, uvs) [tex].Sample([sampler], [uvs]) texture([tex], [uvs])
nkSampleBias(tex, sampler, uvs, bias) [tex].SampleBias([sampler], [uvs], [bias]) texture([tex], [uvs], [bias])
nkSampleLevel(tex, sampler, uvs, level) [tex].SampleLevel([sampler], [uvs], [level]) textureLod([tex], [uvs], [level])
nkLoad(tex, texel) [tex].Load([texel]) texelFetch([tex], [texel.xy], [texel.z])
nkLoadOffset(tex, texel, offset) [tex].Load([texel], [offset])

texelFetchOffset([tex], [texel.xy], [texel.z], [offset])

Constant types
nkConstantBuffer(name, slot) cbuffer [name] : register(b[slot])

layout(binding = [slot], std140) uniform [name]

Comparison functions
nkLessThan(a, b) ([a] < [b])

lessThan([a], [b])

General functions
nkMul(a, b) mul([a], [b]) ([a] * [b])
nkAtan2(a, b) atan2([a], [b]) atan([a], [b])
nkFrac(a) frac([a]) fract([a])
nkSaturate(a) saturate([a]) clamp([a], 0, 1)
nkDdx(a) ddx([a]) dFdx([a])
nkDdy(a) ddy([a]) dFdy([a])
nkLerp(a, b, c) lerp([a], [b], [c]) mix([a], [b], [c])
Parameters
valueWhether the defines should be set (true) or not (false).

◆ prepareMemoryWithFiles()

bool nkGraphics::Program::prepareMemoryWithFiles ( )

If the program is defined from a folder, prepares the memory with the source files content.

Returns
If the loading went well (true) or not (false).

◆ loadFromMemory()

virtual bool nkGraphics::Program::loadFromMemory ( )
pure virtual

Loads the program, from the memory currently set.

Returns
If the loading went well (true) or not (false).

◆ tryToLoadFrom()

virtual bool nkGraphics::Program::tryToLoadFrom ( ProgramSourcesHolder newMemory)
pure virtual

Tries to load from a provided memory, and swap the current state with the newly submitted state only on success. If it fails, the program state is not changed.

Parameters
newMemoryThe memory to try to load from.
Returns
If the loading went well (true) or not (false).

◆ load()

virtual bool nkGraphics::Program::load ( )
overridevirtual

Loads and compiles the program.
When processing the compilation, a program will automatically add some defines to help with cross renderer development :

  • NK_HLSL : in case of a DirectX based renderer
  • NK_GLSL : in case of an OpenGL based renderer


Allowing to write code that will differentiate between both compilers :

#if defined(NK_HLSL)
    float4 main (in float4 pos : SV_POSITION) : SV_TARGET
    {
        return 1 ;
    }
#elif defined(NK_GLSL)
    out vec4 color ;
    void main ()
    {
        color = vec4(1) ;
    }
#endif
Returns
If the loading went well (true) or not (false).

Implements nkGraphics::Resource.

◆ unload()

virtual void nkGraphics::Program::unload ( )
overridevirtual

Unloads and frees the internal resources of the program.

Implements nkGraphics::Resource.

◆ exportClassToTree()

virtual void nkGraphics::Program::exportClassToTree ( nkExport::Node rootNode)
overridevirtual

Basic exporting capabilities.

Parameters
rootNodeThe tree to export to.

Implements nkExport::Exportable.

◆ importClassFromTree()

virtual void nkGraphics::Program::importClassFromTree ( nkExport::Node rootNode)
overridevirtual

Basic importing capabilities.

Parameters
rootNodeThe tree to import from.

Implements nkExport::Exportable.


The documentation for this class was generated from the following file: